//
//  NearbyViewController.m
//  BaChat
//
//  Created by dllo on 16/6/15.
//  Copyright © 2016年 刘洋. All rights reserved.
//

#import "NearbyViewController.h"
#import <MAMapKit/MAMapKit.h>
#import <AMapFoundationKit/AMapFoundationKit.h>
#import <AMapSearchKit/AMapSearchKit.h>

#define APIKey @"19f050897241dbae6bcec94adab45f93"


@interface NearbyViewController ()<MAMapViewDelegate,AMapSearchDelegate>
@property(nonatomic,strong)MAMapView *mapView;
@property(nonatomic,strong)UIButton *locationButton;
@property(nonatomic,strong)AMapSearchAPI *search;
@property(nonatomic,strong)CLLocation *currentlocation;


@end

@implementation NearbyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self initMapView];
    [self initLocationButton];
    [self initSearch];

}

-(void)initMapView{
    //设置APIKey
    
    [AMapServices sharedServices].apiKey = APIKey;
    
    
    self.mapView = [[MAMapView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds))];
    
    self.mapView.delegate = self;
    //罗盘位置
    self.mapView.compassOrigin = CGPointMake(self.mapView.compassOrigin.x, 22);
    //比例尺位置
    self.mapView.scaleOrigin = CGPointMake(self.mapView.scaleOrigin.x, self.mapView.compassOrigin.y);
    [self.view addSubview:self.mapView];
    
    //打开用户定位
    self.mapView.showsUserLocation = YES;
    
    //设置跟踪模式
    [self.mapView setUserTrackingMode:MAUserTrackingModeFollow animated:YES];
    
}

//创建查询

/*!
 @brief 地址编码查询接 。
 @param request 查询选项。具体属性字段请参考 AMapGeocodeSearchRequest 类。
 - (void)AMapGeocodeSearch:(AMapGeocodeSearchRequest *)request;
 */

/*!
 @brief 逆地址编码查询接 。
 @param request 查询选项。具体属性字段请参考 AMapReGeocodeSearchRequest 类。
 - (void)AMapReGoecodeSearch:(AMapReGeocodeSearchRequest *)request;
 */

-(void)initSearch{
    
    self.search = [[AMapSearchAPI alloc] init];
    self.search.delegate = self;
    
}


//当位置更新时，会进定位回调，通过回调函数，能获取到定位点的经纬度坐标
-(void)mapView:(MAMapView *)mapView didUpdateUserLocation:(MAUserLocation *)userLocation updatingLocation:(BOOL)updatingLocation{
    if (updatingLocation) {
        NSLog(@"latitude : %f,longitude: %f",userLocation.coordinate.latitude,userLocation.coordinate.longitude);
        //        CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(latitudeLanOu, longitudeLanOu);
        // b) 设置缩放
        // MACoordinateSpan span = MACoordinateSpanMake(0.001, 0.001);
        
        // c) 设置区域
        // MACoordinateRegion region = MACoordinateRegionMake(coordinate, span);
        // d) 显示区域
        //  _mapView.region = region;
        //
        
        self.currentlocation = [userLocation.location copy];
        
    }
}

//逆地理编码 发起搜索请求
-(void)reGeoAction{
    if (self.currentlocation) {
        AMapReGeocodeSearchRequest *request = [[AMapReGeocodeSearchRequest alloc] init];
        request.location = [AMapGeoPoint locationWithLatitude:self.currentlocation.coordinate.latitude longitude:self.currentlocation.coordinate.longitude];
        [self.search AMapReGoecodeSearch:request];
    }
    
}


-(void)onReGeocodeSearchDone:(AMapReGeocodeSearchRequest *)request response:(AMapReGeocodeSearchResponse *)response{
    NSString *title = response.regeocode.addressComponent.city;
    if (title.length == 0) {
        title = response.regeocode.addressComponent.province;
    }
    
    self.mapView.userLocation.title = title;
    self.mapView.userLocation.subtitle = response.regeocode.formattedAddress;
    
}

//在选中用户位置annotation时弹出当前地址
-(void)mapView:(MAMapView *)mapView didSelectAnnotationView:(MAAnnotationView *)view{
    // 选中定位annotation的时候进行逆地理编码查询
    if ([view.annotation isKindOfClass:[MAUserLocation class]]) {
        
        [self reGeoAction];
    }
    
}


//创建定位按钮
-(void)initLocationButton{
    self.locationButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.locationButton.frame = CGRectMake(20, CGRectGetHeight(self.mapView.bounds) - 80, 30, 30);
    self.locationButton.layer.masksToBounds = YES;
    self.locationButton.layer.cornerRadius = 5;
    
    [self.locationButton addTarget:self action:@selector(locateAction) forControlEvents:UIControlEventTouchUpInside];
    [self.locationButton setImage:[UIImage imageNamed:@"location_yes@2x"] forState:UIControlStateNormal];
    [self.mapView addSubview:self.locationButton];
    
}

//开启或关闭定位
-(void)locateAction{
    if (self.mapView.userTrackingMode != MAUserTrackingModeFollow) {
        [self.mapView setUserTrackingMode:MAUserTrackingModeFollow animated:YES];
    }else{
        [self.mapView setUserTrackingMode:MAUserTrackingModeNone animated:YES];
        
    }
    
}

//替换定位按钮图标:使用mapView回调方法监听定位模式状态
-(void)mapView:(MAMapView *)mapView didChangeUserTrackingMode:(MAUserTrackingMode)mode animated:(BOOL)animated{
    if (mode ==  MAUserTrackingModeNone) {
        [self.locationButton setImage:[UIImage imageNamed:@"location_no"] forState:UIControlStateNormal];
    }else{
        
        [self.locationButton setImage:[UIImage imageNamed:@"location_yes"]forState:UIControlStateNormal];
        
    }
    
    
}






@end
